<?php
/**
 * Restore password controller
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Usermanagement Module
 * @subpackage Controller
 * @license  New BSD License
 *
 */
class usermanagement_PassrestoreController extends Bel_Controller  {
	
	/**
	 * Displays form to provide email address to send restore instructions to
	 *
	 */
	public function indexAction() {
		$this->_seo->setTitle('Restore Password');
		$form = new Bel_Forms_Builder ( 'passrestore_form', '/usermanagement/passrestore/sendrestoreemail' );
		$this->view->assign ( 'form', $form );
		$this->view->display ( 'forms/default.tpl' );
	}

	/**
	 * Send email with instructions to provided email. If email was not found in 
	 * DB error is generated
	 *
	 */
	public function sendrestoreemailAction() {		
		$request = $this->_request->getParams ();
		$error_flag = true;
		$errors = array ( );
		
		$oUsers = new Models_Usermanagement_Users();
		
		if (!($user_data=$oUsers->getUserByEmail($request ['user_email']))){
			$error_flag = false;
			$errors [] = 'No user with such email';
		}
		
		$oH = new Helpers_Usermanagement_Validate();
		
		if (! $oH->validateCaptcha( $request )) {
			$error_flag = false;
			$errors [] = 'Wrong captcha field';
		}
		
		if ($this->_validator->validate ( 'passrestore_form', $request ) && $error_flag) {
			$code = md5 ( $user_data->user_email.$user_data->user_id);
			$this->_sendPassresetEmail($user_data->user_email, $user_data->user_login, $code);
			$user_data->user_confirmation_code = $code;
			$user_data->save ();
			$this->_messages->setMessage('Please check your email for instructions on how to reset your password.');
			$this->_redirect ( '/' );
		} else {
			$errors = array_merge ( $this->_validator->getErrors (), $errors );
			foreach ( $errors as $error ) {
				$this->_messages->setMessage ( $error ,'error');
			}
			$this->_redirect ( '/restore_pass/' );
		}
	}
	
	/**
	 * Displays form to enter new password.
	 * Code from restoration email is inserted as hidden field to this form
	 *
	 */
	public function resetformAction() {
		$this->_seo->setTitle('Enter New Password');
		$form = new Bel_Forms_Builder('passreset_form','/usermanagement/passrestore/reset');
		$form->addElement('hidden','code',array('value'=>$this->_request->getParam ( 'code' )));
		$this->view->assign('form',$form);
		$this->view->display ( 'forms/default.tpl' );
	}
	
	/**
	 * Resets password to new after code validation
	 *
	 */
	public function resetAction() {
		$request = $this->_request->getParams ();
		$error_flag = true;
		$errors = array ( );		
		$users = new Models_Usermanagement_Users ( );
		
		$user = $users->getUserForResetPass ( $request ['code'] );
		
		if (! $user) {
			$error_flag = false;
			$errors [] = 'Reset pass error';
		}
		
		$oH = new Helpers_Usermanagement_Validate();
		
		if (!$oH->ifPasswordMatch ( $request ['user_password'], $request ['password2'] )) {
			$error_flag = false;
			$errors ['password'] = 'Passwords do not match';
		}
		
		if ($this->_validator->validate ( 'passreset_form', $request ) && $error_flag) {
			
			$user->user_confirmation_code = NULL;
			$user->user_password = md5($request ['user_password']);
			$user->save ();
			
			$this->_messages->setMessage ( 'Password has been reseted' );
			$this->_redirect ( '/' );
		
		} else {
			$errors = array_merge ( $this->_validator->getErrors (), $errors );
			foreach ( $errors as $error ) {
				$this->_messages->setMessage ( $error, 'error' );
			}
			$this->_redirect ( '/restore_pass/' );
		}
	}
	
	/**
	 * Handles emailing
	 *
	 * @param string $email_to
	 * @param string $name_to
	 * @param string $confirmation_code
	 */
	private function _sendPassresetEmail($email_to, $name_to, $confirmation_code){
		$mailer = new Bel_Mailer();
		$mailer->addTo ( $email_to );
		$mailer->setSubject('Lost password recovery instructions');
		$this->view->assign('code',$confirmation_code);
		$this->view->assign('name',$name_to);
		$mailer->setBodyHtml ( $this->view->fetch('usermanagement/restore/restore_email.tpl') );
		$mailer->send ();
	}	
}